const injectRef = Object.getPrototypeOf(global) || global
injectRef.regeneratorRuntime = require('@babel/runtime/regenerator')
import prompt from '@system.prompt'

// abilityType: 0-Ability; 1-Internal Ability
const ABILITY_TYPE_EXTERNAL = 0;
const ABILITY_TYPE_INTERNAL = 1;
// syncOption(Optional, default sync): 0-Sync; 1-Async
const ACTION_SYNC = 0;
const ACTION_ASYNC = 1;
const ACTION_MESSAGE_CODE_PLUS = 1001;

export default {
    data: {
        continueAbilityData: {
            remoteData1: 'self define continue data for distribute',
            remoteData2: {
                item1: 0,
                item2: true,
                item3: 'inner string'
            },
            remoteData3: [1, 2, 3]
        }
    },
    plus: async function() {
        var actionData = {};
        actionData.firstNum = 1024;
        actionData.secondNum = 2048;

        var action = {};
        action.bundleName = 'com.it360.hms_js';
        action.abilityName = 'com.it360.hms_js.ComputeServiceAbility';
        action.messageCode = ACTION_MESSAGE_CODE_PLUS;
        action.data = actionData;
        action.abilityType = ABILITY_TYPE_EXTERNAL;
        action.syncOption = ACTION_SYNC;

        var result = await FeatureAbility.callAbility(action);
        var ret = JSON.parse(result);
        if (ret.code == 0) {
          console.info('plus result is:' + JSON.stringify(ret.abilityResult));
        } else {
          console.error('plus error code:' + JSON.stringify(ret.code));
        }
    },
// shareData的数据会在onSaveData触发时与saveData一起传送到迁移目标FA，并绑定到其shareData数据段上
// shareData的数据可以直接使用this访问。eg:this.remoteShareData1
    shareData: {
        remoteShareData1: 'share data for distribute',
        remoteShareData2: {
            item1: 0,
            item2: false,
            item3: 'inner string'
        },
        remoteShareData3: [4, 5, 6]
    },
    tryContinueAbility: async function() {
        // 应用进行迁移
        let result = await FeatureAbility.continueAbility();
        console.info("result:" + JSON.stringify(result));
    },
    onStartContinuation: function onStartContinuation() {
        // 判断当前的状态是不是适合迁移
        console.info("trigger onStartContinuation");
        return true;
    },
    onCompleteContinuation: function onCompleteContinuation(code) {
        // 迁移操作完成，code返回结果
        console.info("trigger onCompleteContinuation: code = " + code);
    },
    onSaveData: function onSaveData(saveData) {
        // 数据保存到savedData中进行迁移。
        var data = this.continueAbilityData;
        Object.assign(saveData, data)
    },
    onRestoreData: function onRestoreData(restoreData) {
        // 收到迁移数据，恢复。
        this.continueAbilityData = restoreData;
    }
}